// 路由管理
// 浏览器打开进行加载，一般只进行首页的加载
import {createRouter,createWebHistory,createWebHashHistory} from "vue-router"
import store from "@/store";
import api from "@/api";
const routes =[

    //带跳转参数，参数不丢失
    // {
    //     path:"/detail/:id",
    //     name:"detail",
    //     component:()=>import("@/components/detail/Detail.vue")
    // },

    {
        path:"/login",
        name:"login",
        component:()=>import("@/components/login/Login.vue"),
        meta: {  isAuth: true,title:'登录'},
        beforeEnter: (to, from, next) => {
            if (to.meta.isAuth) { //判断是否需要授权
                if (localStorage.getItem('AUTHORIZATION_ADMIN_TOKEN') == null) {
                    next()  //放行
                }
            }
        }
    },
    {
        path:"/",
        name:"home",
        component:()=>import("@/components/home/Home.vue"),
        meta: {
            isAuth: true,title: '欢迎使用博客管理系统-首页',
        },

    },
    {
        path:"/home",
        name:"home",
        component:()=>import("@/components/home/Home.vue"),
        meta: {
            isAuth: true,title: '欢迎使用博客管理系统-首页',
        },
        redirect:"/overview",
        children:[
            {
              path: "/overview",
              name:"overview",
              component:()=>import("@/components/Overview/OverView.vue"),
              meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-首页',
                },
            },
            {
                path:"/greatPictures",
                name:"greatPictures",
                component:()=>import("@/components/greatPictures/GreatPictures.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-精美图片',
                },
            },
            {
                path: "/userInformationEditing",
                name:"userInformationEditing",
                component:()=>import("@/components/userInformation/UserInformationEditing.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-用户信息编辑',
                },
            },
            {
                path: "/operationalLogs",
                name:"operationalLogs",
                component:()=>import("@/components/operationalLogs/operationalLogs.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-操作日志',
                },
            },
            {
                path: "/messageBoards",
                name:"messageBoards",
                component:()=>import("@/components/messageBoards/MessageBoards.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-留言管理',
                },
            },
            {
                path:"/announcement",
                name:"announcement",
                component:()=>import("@/components/announcement/Announcement.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-发布公告',
                },
            },
            {
                path:"/comments",
                name:"comments",
                component:()=>import("@/components/comments/Comments.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-评论管理',
                },

            },
            {
                path: "/articleInformationEditing",
                name:"articleInformationEditing",
                component:()=>import("@/components/ArticleInformation/ArticleInformationEditing.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-文章信息编辑',
                },
            },
            {
                path:"/privateSpace",
                name:"privateSpace",
                component:()=>import("@/components/privateSpace/PrivateSpace.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-点点滴滴',
                    beforeEnter: (to, from, next) => {
                        if (to.meta.isAuth) { //判断是否需要授权
                           let user = JSON.parse(localStorage.getItem('user'));
                            if (user.uid === 1) {
                                next()  //放行
                            }else {
                                alert("您没有此权限")
                            }
                        }
                    }
                },
            },
            {
                path:"/blessingMessage",
                name:"blessingMessage",
                component:()=>import("@/components/privateSpace/BlessingMessage.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-祝福留言',
                    beforeEnter: (to, from, next) => {
                        if (to.meta.isAuth) { //判断是否需要授权
                            let user = JSON.parse(localStorage.getItem('user'));
                            if (user.uid === 1) {
                                next()  //放行
                            }else {
                                alert("您没有此权限")
                            }
                        }
                    }
                },
            },
            {
                path:"/loveEvents",
                name:"loveEvents",
                component:()=>import("@/components/privateSpace/LoveEvents.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-承诺约定',
                    beforeEnter: (to, from, next) => {
                        if (to.meta.isAuth) { //判断是否需要授权
                            let user = JSON.parse(localStorage.getItem('user'));
                            if (user.uid === 1) {
                                next()  //放行
                            }else {
                                alert("您没有此权限")
                            }
                        }
                    }
                },
            },
            {
                path:"/privateSpace",
                name:"privateSpace",
                component:()=>import("@/components/privateSpace/PrivateSpace.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-点点滴滴',
                    beforeEnter: (to, from, next) => {
                        if (to.meta.isAuth) { //判断是否需要授权
                            let user = JSON.parse(localStorage.getItem('user'));
                            if (user.uid === 1) {
                                next()  //放行
                            }else {
                                alert("您没有此权限")
                            }
                        }
                    }
                },
            },
            {
                path:"/administratorSettings",
                name:"administratorSettings",
                component:()=>import("@/components/administratorSettings/Administrator.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-管理员设置',
                },
            },
            {
                path:"/userInformation",
                name:"userInformation",
                component:()=>import("@/components/userInformation/UserInformation.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-用户信息',
                },

            },
            {
                path:"/announcement",
                name:"announcement",
                component:()=>import("@/components/announcement/Announcement.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-发布公告',
                },
            },
            {
                path:"/articleInformation",
                name:"articleInformation",
                component:()=>import("@/components/ArticleInformation/ArticleInformation.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-文章信息',
                }
            },
            {
                path:"/websiteAdministration",
                name:"websiteAdministration",
                component:()=>import("@/components/WebsiteAdministration/WebsiteAdministration.vue"),
                meta: {
                    isAuth: true,title: '欢迎使用博客管理系统-网站管理',
                }
            },


        ],
    },


    // {
    //     path:"/articleEditing",
    //     name:"articleEditing",
    //     component:()=>import("@/components/articleEditing/ArticleEditing.vue"),
    //     meta: {isAuth: true,title:'我的创作'},
    //     beforeEnter: (to, from, next) => {
    //         if (to.meta.isAuth) { //判断是否需要授权
    //             if (localStorage.getItem('user') !== null) {
    //                 next()  //放行
    //             } else {
    //                 alert('抱歉，请先登录！')
    //                 next("/login");
    //             }
    //         } else {
    //             next()  //放行
    //         }
    //     }
    // },

];
const router = createRouter({
    //createWebHashHistory()  不需要后端配合  类似于a标签的描点连接
    history:createWebHashHistory(),
    // history:createWebHashHistory(),
    routes,
});

// 全局导航守卫 (切换路由前)
router.beforeEach((to,from,next)=>{

    //获取浏览器里存储的token
    const tokenStr = localStorage.getItem("AUTHORIZATION_ADMIN_TOKEN");//读取浏览器的token
    //没有token则应该删除用户的信息
    if(!tokenStr){
        localStorage.clear();
        if( to.path!=="/login"){
            next("/login");  //强制要求跳转到登录页
        }else {
            next();
        }
    }else {
        next();
    }


})
// 全局导航守卫 (切换路由后)
router.afterEach((to, from) => {
    document.title = to.meta.title || '默认名'    //修改网页的title

})



export default router;